package com.sfac.springboot.modules.test.controller;

import com.sfac.springboot.modules.common.vo.Result;
import com.sfac.springboot.modules.common.vo.Search;
import com.sfac.springboot.modules.test.entity.Student;
import com.sfac.springboot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class StudentController {
    @Autowired
    private StudentService studentService;


    /**
     * 127.0.0.1/api/student  ----post
     * {"studentName":"wxx"}
     * {"studentName":"wxx","studentCard":{"cardNum":"202250111"}}
     */
    @PostMapping(value = "/student",consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student){
        return studentService.insertStudent(student);
    }

    /**
     * 127.0.0.1/api/student  ----put
     * {"id":1,"studentName":"wxx"}
     * {"studentName":"wxx","studentCard":{"cardNum":"202250111"}}
     */
    @PutMapping(value = "/student",consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student){
        return studentService.updateStudent(student);
    }

    /**
     * 127.0.0.1/api/student/13  ----delete
     */
    @DeleteMapping(value = "/student/{id}")
    public Result<Object> deleteStudent(@PathVariable int id){
        return studentService.deleteStudent(id);
    }

    /**
     * 127.0.0.1/api/student/2  ----get
     */
    @GetMapping(value = "/student/{id}")
    public Student getStudentById(@PathVariable int id){
        return studentService.getStudentById(id);
    }

    /**
     * 127.0.0.1/api/students ----post
     * {"currentPage":1,"pageSize":5,"sort":"studentName","direction":"desc","keyword":""}
     */
    @PostMapping(value = "/students",consumes = "application/json")
    public Page getStudentsBySearch(@RequestBody Search search){
        return studentService.getStudentsBySearch(search);
    }

    /**
     * 127.0.0.1/api/students?studentName=wxx1 ----post
     */
    @GetMapping("/students")
    public List<Student> getStudentByStudentName(@RequestParam String studentName){
        return studentService.getStudentByStudentName(studentName);
    }
}
